# python数据基础第十节：数据可视化（多条件筛选）
"""
python数据基础第十节：数据可视化
    1、多条件筛选：

"""
# 导出数据的模块在DataFrame模块下
from pandas import DataFrame, read_csv, to_datetime
import pandas
import matplotlib.pyplot as plt
import matplotlib
import numpy
from datetime import datetime

# 多条件柱状图
df4 = read_csv("E:\Python\pyspark_demo01\out_data\data16.csv")
font = {"family":"SimHei"}
matplotlib.rc("font",**font)
d1 = "手机品牌"
d2 = "通信品牌"
v = "月消费"

gb= df4.groupby(by=[d1,d2])["月消费（元）"].agg({v:numpy.sum})

d1size = gb.index.levels[0].size
d2size = gb.index.levels[1].size

index = numpy.arange(d1size)
colors=["r","g","b"]

for i in range(0,d2size):
    print(i)
    subgb = gb[v][gb.index.labels[1]==i]
    bar = plt.bar(index*d2size + i,subgb,color=colors[i])
lIndex = numpy.arange(d1size)*d2size
plt.xticks(lIndex + 3/2,gb.index.levels[0])
plt.legend(gb.index.levels[1])
plt.show()


# 只在一条柱型图上显示多条数据
bsum = index*0.0
for i in range(0,d2size):
    print(i)
    subgb = gb[v][gb.index.labels[1]==i]
    bar1 = plt.bar(index,subgb,color=colors[i],bottom=bsum)
    bsum +=subgb

plt.xticks(index+1/2,gb.index.levels[0])
plt.legend(gb.index.levels[0])
plt.show()